package org.cresource.org.service.impl;

import org.cresource.basic.service.impl.BaseServiceImpl;
import org.cresource.basic.util.PageList;
import org.cresource.org.domain.Department;
import org.cresource.org.mapper.DepartmentMapper;
import org.cresource.org.query.DepartmentQuery;
import org.cresource.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void add(Department department) {
        departmentMapper.save(department);
        this.update(department);
    }

    @Override
    public void update(Department department) {
        if(department.getParent() == null){
            department.setDirPath("/"+department.getId());
        }else {
            String parentDirPath = departmentMapper.loadById(department.getParent().getId()).getDirPath();
            String myDirpath = parentDirPath + "/" + department.getId();
            department.setDirPath(myDirpath);
        }
        departmentMapper.update(department);
    }

    @Override
    public List<Department> queryDeptTree() {
        return departmentMapper.loadDeptTree();
    }
}
